/*
* @Author: Administrator
* @Date:   2018-07-18 22:54:35
* @Last Modified by:   Administrator
* @Last Modified time: 2018-07-19 23:30:16
*/
/*
* @Author: admin
* @Date:   2018-07-17 10:00:30
* @Last Modified by:   admin
* @Last Modified time: 2018-07-18 16:29:07
*/
const express = require('express');
const app = express();
const db = require('./db.js')
const MongoClient = require('mongodb').MongoClient;
const fs = require('fs');
const url2 = require('url');
const path = require('path');
const querystring = require('querystring');
const BSON = require('bson').BSONPure;
var url = "mongodb://127.0.0.1:27017";
var t = 0;
var num = 0;
// 设置一页多少条数据
var pageSize = 5;
var resultdata = '';

// 获取当前日期函数
function getNowDate() {
	var date = new Date();
	var sign1 = "/";
	var sign2 = ":";
	var year = date.getFullYear() // 年
	var month = date.getMonth() + 1; // 月
	var day  = date.getDate(); // 日
	var hour = date.getHours(); // 时
	var minutes = date.getMinutes(); // 分
	var seconds = date.getSeconds() //秒
	// 给一位数数据前面加 “0”
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (day >= 0 && day <= 9) {
		day = "0" + day;
	}
	if (hour >= 0 && hour <= 9) {
		hour = "0" + hour;
	}
	if (minutes >= 0 && minutes <= 9) {
		minutes = "0" + minutes;
	}
	if (seconds >= 0 && seconds <= 9) {
		seconds = "0" + seconds;
	}
	var currentdate = year + sign1 + month + sign1 + day +'-' + hour + sign2 + minutes + sign2 + seconds;
	return currentdate;
}

// 一开始加载./static文件夹下的index.html
app.use(express.static('./static'));

// 加载所有数据
app.get('/all',function(req,res){
	res.writeHead(200,{"Content-Type":"test/html;charset = utf8"});
	MongoClient.connect(url,function(err,db){
		if(err) throw err;
		console.log('数据库连接成功')
		// 数据库名字
		var dbase = db.db('record');
		// 数据库集合  按时间顺序倒序排序
		dbase.collection('myrecord').find().limit(pageSize).sort({time:-1}).toArray(function(err,result){
			var re ={
				"page":{
					"currentPage":1,//当前页
					"pageSize":pageSize,//一页多少条数据
					"dataCount":26//数据总量
				},
				"result":result//结集
			};
			res.end(JSON.stringify(re));
		})	
		db.close()
	})

});

// 分页
app.get('/getPage',function(req,res){ 
	res.writeHead(200,{"Content-Type":"test/html;charset = utf8"});  
	// 上一页
	if(parseInt(querystring.parse(url2.parse(req.url).query).page) == -1){
		if(num == 0){
			num =0;
		}else{
			num = --t;
		}	
		// 下一页
	}else if(parseInt(querystring.parse(url2.parse(req.url).query).page) == -2){
		if(num == 6){
			num = 6
		}else{
			num = ++t;
		}
		
	}else{
		num = parseInt(querystring.parse(url2.parse(req.url).query).page)-1;
	}	
	MongoClient.connect(url,function(err,db){
		if(err) throw err;
		console.log('数据库连接成功')
		var dbase = db.db('record');
		dbase.collection('myrecord').find().limit(pageSize).skip(pageSize*num).sort({time:-1}).toArray(function(err,result){
			var re ={
				"page":{
					"currentPage":parseInt(num+1),//当前页
					"pageSize":pageSize,//一页多少条数据
					"dataCount":dbase.collection('myrecord').count()//数据总量
				},
				"result":result//结集
			};
			res.end(JSON.stringify(re));
		})	
		db.close()
	})
	// 记录上一页的num
	t = num; 

})

// 新增数据
app.get('/addList',function(req,res){
	// 获取发布内容
	var con = (querystring.parse(url2.parse(req.url).query)).con;
	// 创建一个对象 一条数据
	var myobj = {
		"name" : "mike",
		"con" : con,
		"time" : getNowDate()
	}
	// 数据库中插入数据
	db.insert('record','myrecord',myobj,function(err,result){
		// 重新获得数据库的数据
		db.find('record','myrecord',{time:-1},pageSize,num,function(err,result){
			res.end(JSON.stringify(result))
		})
	})
})

// 删除数据
app.get('/delList',function(req,res){
	// 获取妖删除的_id
	console.log(querystring.parse(url2.parse(req.url).query));
	var _id = (querystring.parse(url2.parse(req.url).query))._id;
	console.log('_id:'+_id)
	var obj_id = BSON.ObjectID.createFromHexString(_id)
	console.log(_id)
	// 根据_id删除数据库数据
	db.delete('record','myrecord',{_id : obj_id},function(err,result){
		// 重新获得数据库的数据
		db.find('record','myrecord',{time:-1},pageSize,num,function(err,result){
			res.end(JSON.stringify(result))
		})
	})
})

app.listen(8989);

